package org.caw.hbase.explorer.filter.model;

import java.util.HashSet;
import java.util.Set;

import org.caw.exception.CawParseException;

/**
 * 
 * @author dino.keco
 * 
 */
public class OperatorNode extends Node {

	/** used for validation that on one level all nodes have same operator */
	private Set<LogicalOperator> operators;

	public Set<LogicalOperator> getOperators() {
		return operators;
	}

	public void setOperators(Set<LogicalOperator> operators) {
		this.operators = operators;
	}

	public void addOperator(String operator) throws CawParseException {
		if (operators == null) {
			operators = new HashSet<LogicalOperator>();
		}
		if (operator != null) {

			try {
				operators.add(LogicalOperator.valueOf(operator.trim()));
			} catch (Exception e) {
				throw new CawParseException("Invalid logical operator " + operator);
			}

			if (operators.size() > 1) {
				throw new CawParseException("Can't mix logical operators at same level. Please use parentheses.");
			}
		}
	}

	public LogicalOperator getLogicaOperator() throws CawParseException {
		if (operators != null && !operators.isEmpty()) {
			return operators.iterator().next();
		} else {
			throw new CawParseException("No logical operator in expression");
		}
	}

}
